<?php

if( !defined("IN_ELCIUS") ) exit;


class KiwiDB{
	
	private static $link;
	private static $connected;
	
	// TODO: expand table white list, add value filter expression
	private static $tables = array(
		'raiders' => array(
			'Name', 'Characters', 'Comment', 'Position'
		),
	);
	
	public static $Data; // public data, write-only, untrustworthy scum
	
	public static function Initialize(){
		self::$connected = false;
		self::$Data = array(
			'Connected' => false,
		);
	}
	
	public static function Connect(){
		global $Settings;
		
		self::$connected = false;
		self:$Data['Connected'] = false;
		
		if( !self::$link ) {
			self::$link = new pdo(
				'mysql:host='.$Settings['MySQL_host'].';dbname='.$Settings['Kiwi_database'],
				$Settings['MySQL_username'],
				$Settings['MySQL_password']
			);
			if ( self::$link ){
				self::$connected = true;
				self::$Data['Connected'] = true;
			}
		}
		
		return self::$connected;
	}
	
	
	public static function Get( $table, $n = 10, $i = 0 ){
		
		$return = array();
		
		if( self::$connected or self::Connect() ){
			
			$table = preg_replace( "/[^\p{L}_0-9]+/u", '', $table );
			if( !is_numeric($i) ) $i = 0;
			if( !is_numeric($n) ) $n = 10;
			
			$result = self::$link->prepare( "SELECT * FROM `$table` LIMIT $i, $n" );
			$result->execute( array( $table ) );
			
			if( $result ){
				while( $row = $result->Fetch( PDO::FETCH_ASSOC ) ){
					$return[] = $row;
				}
			}
			
		}
		return $return;
	}
	
	public static function Close(){
		self::$connected = false;
		self:$Data['Connected'] = false;
		// TODO: DC
	}
	
	
	
	public static function Set( $table, $data ){
		
		if( KiwiAuth::IsEditor() == true and ( self::$connected or self::Connect() ) ){
			if( isset( self::$tables[ $table ] ) ){
				
				// put valid fields/values into arrays
				$keys = array();
				$values = array();
				foreach( $data as $k => $v ){
					if( in_array( $k, self::$tables[ $table ] ) ){
						$keys[] = $k;
						$values[] = $v;
					}
				}
				
				if( !empty( $keys ) and !empty( $values ) ){
					return self::$link->prepare( 'REPLACE INTO `'.$table.'` (`'.implode( '`,`', $keys ).'`) VALUES ( '.trim( str_repeat('?,',count($values)), ',' ).' );' )->execute( $values );
				}
				
			}
		}
		return false;
	}
	
	public static function Delete( $table, $data ){
		
		if( KiwiAuth::IsEditor() == true and ( self::$connected or self::Connect() ) ){
			if( isset( self::$tables[ $table ] ) ){
				
				// put valid fields/values into arrays
				$keys = array();
				$values = array();
				foreach( $data as $k => $v ){
					if( in_array( $k, self::$tables[ $table ] ) ){
						$keys[] = "`$k` = ?";
						$values[] = $v;
					}
				}
				
				if( !empty( $keys ) and !empty( $values ) ){
					return self::$link->prepare( 'DELETE FROM `'.$table.'` WHERE '.implode( ' AND ', $keys ) )->execute( $values );
				}
				
			}
		}
		return false;
	}
	
}

?>